package com.mapps.jurado.fragments;

import com.mapps.jurado.PrincipalActivity;
import com.mapps.jurado.R;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ShareActionProvider;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class NotaFragment extends Fragment {

	private String nota;
	private TextView textViewNota;
	private RelativeLayout layout;
	private MediaPlayer player;
	
	private ShareActionProvider mShareActionProvider;
	
	public NotaFragment(String nota){
		this.nota = nota;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.layout_principal, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		textViewNota = (TextView) getView().findViewById(R.id.textViewNota);
		layout = (RelativeLayout) getView().findViewById(R.id.relativeLayoutPrincipal);
		setHasOptionsMenu(true);
		player = new MediaPlayer();
		
		textViewNota.setText(nota);
		int notaInt = Integer.parseInt(nota);
		if(notaInt == 0 || notaInt < 6){
			layout.setBackgroundColor(Color.parseColor("#FF4444"));
			player = MediaPlayer.create(getActivity(), R.raw.som_grilo);
			player.start();
		} else if(notaInt >= 6 && notaInt <= 9){
			layout.setBackgroundColor(Color.parseColor("#99CC00"));
			player = MediaPlayer.create(getActivity(), R.raw.aplauso_curto);
			player.start();
		} else {
			layout.setBackgroundColor(Color.parseColor("#33B5E5"));
			player = MediaPlayer.create(getActivity(), R.raw.aplauso_longo);
			player.start();
		}
		PrincipalActivity.manipulaMenu();
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.menu_nota, menu);
		
		MenuItem item = menu.findItem(R.id.menuItemShare);
		mShareActionProvider = (ShareActionProvider) item.getActionProvider();
		mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
		mShareActionProvider.setShareIntent(createShareIntent());
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	private Intent createShareIntent(){
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_TEXT, "Eu avaliei algo com a nota \"" + nota + "\" no aplicativo O Jurado");
		return shareIntent;
	}
	
}
